<template>
  <el-dialog
    class="reset-dialog"
    title="重置身份验证码"
    :visible.sync="visible"
    width="50%"
    :before-close="handleClose">
    <el-row :gutter="10">
      <el-col :span="3" align="right">账户名</el-col>
      <el-col :span="5">
        <el-input type="text" disabled="" :value="rowData.username"></el-input>
      </el-col>
    </el-row>
    <el-row :gutter="10">
      <el-col :span="3" align="right">二维验证码</el-col>
      <el-col :span="17">
        <el-input type="text" v-model="secret" ref="codeInput"></el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="handleGetCode">重置验证码</el-button>
      </el-col>
    </el-row>
    <el-row :gutter="10">
      <el-col :offset="3">
        <el-button type="primary" @click.native="handleCopy">复制</el-button>
      </el-col>
    </el-row>
    <el-row v-if="codeShow">
      <el-col :offset="3">
        <qriously ref="qrcode" :value='googleValidate' :size='190' id="mycanvas"></qriously>
      </el-col>
    </el-row>
    <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="handleClose">关 闭</el-button>
  </span>
  </el-dialog>
</template>

<script>
import { authencationCodeGetApi, resetCodeApi } from '@/services/api/permission';
import { grid } from '@/build';

export default {
  name: 'reset-authentication-code',
  mixins: [grid],
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    rowData: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      visible: false,
      code: undefined,
      authencationCodeData: {},
      googleValidate: '',
      codeShow: false,
      secret: undefined,
    };
  },
  methods: {
    handleClose() {
      this.$emit('close');
    },
    handleGetCode() {
      this.doGet({ action: 'resetCode' });
    },
    handleCopy() {
      const iptDom = this.$refs.codeInput;
      iptDom.select(); // 选择对象
      document.execCommand('Copy'); // 执行浏览器复制命令
      this.$notify.success({
        title: '复制成功',
        duration: 2000,
      });
    },
  },
  mounted() {
    this.visible = this.dialogVisible;

    // 获取用户现有的验证码
    this.bindGet(authencationCodeGetApi, '', {
      action: 'getCode',
      sendingData: { userId: this.rowData.userId },
      callback: ([result, data]) => {
        if (result && data) {
          this.codeShow = true;
          const { qrcode, secret } = data;
          this.googleValidate = qrcode;
          this.secret = secret;
        } else {
          this.$message({
            message: data.message,
            type: 'warning',
          });
        }
      },
    });

    this.getAll();

    // 重置验证码
    this.bindGet(resetCodeApi, '', {
      action: 'resetCode',
      sendingData: { userId: this.rowData.userId },
      callback: ([result, data]) => {
        if (result) {
          if (!this.codeShow) {
            this.codeShow = true;
          }
          const { qrcode, secret } = data;
          this.googleValidate = qrcode;
          this.secret = secret;
          this.$message({
            message: '重置验证码成功',
            type: 'success',
          });
        } else {
          this.$message({
            message: data.message,
            type: 'error',
          });
        }
      },
    });
  },
};
</script>

<style lang="scss" scoped>
  .reset-dialog {
    .el-row{
      line-height: 28px;
      margin-bottom: 10px;
      &:last-child {
        margin: 0;
      }
    }
  }
</style>
